<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="report_over_flow_ssssabcd"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-配件报溢.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
    :params="fetchParams"
      :datakey="'rows'"-->
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :disabled="printBtn" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <!-- 打印 -->
    <div v-if="showPrint">
      <BasePrint ref="print" :data="printList" template="templatePartOverFlow" />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getProfitItem } from '@/api/part/partInventory/partOverflow';
import { dealerPrintApi } from '@/api/repair/index.js';
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { getUser } from '@/assets/js/auth';
import { exportExcelFile } from '@/utils/index';
import moment from 'moment';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array,
    tax: Number
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [],
      showPrint: true, // 打印是否显示
      printList: {}, // 打印数据对象
      printBtn: false, // 打印按钮
      profitNo: '',
      handlerList: [],
      printParams: {},
      dataSource: []
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      console.log(fetchParamObj, 'fetchParamObj----');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    this.getEmployee();
    // this.getPrintParams();
  },
  methods: {
    // 导出方法
    exportFun(row) {
      for (const key in row) {
        switch (key) {
          // case 'FINISHED_DATE':
          //   // case 'FINISHED_DATE':
          //   // case 'BALANCE_TIME':
          //   // case 'SQUARE_DATE':
          //   if (row[key]) {
          //     row[key] = moment(row[key]).format('YYYY-MM-DD');
          //   }
          //   break;
          case 'CONSULTANT':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          case 'PROFIT_AMOUNT':
            if (row[key]) {
              row[key] = Number(row.COST_AMOUNT * (1 + Number(this.tax))).toFixed(2);
            }
            break;
          case 'PROFIT_QUANTITY':
          case 'COST_PRICE':
          case 'IN_AMOUNT':
          case 'IN_AMOUNT_TAXED':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },
    // 打印数据接口
    async getPrintData() {
      this.profitNo = this.selectColumns[0].PROFIT_NO;
      let profitNo = this.selectColumns[0].PROFIT_NO;
      const res = await getProfitItem({ profitNo }); // 获取当前行数据
      this.printList = res;
      this.printList.profitNo = profitNo;
      console.log(this.printList, 'this.printList22::');
      let userId = await getUser();
      this.printList.USER_NAME = this.getHandlerName(userId);
      console.log(this.printList, 'this.printList11::');
      this.printList.data[0].CREATED_NAME = this.getHandlerName(this.printList.data[0].CREATED_BY);
      console.log(this.printList, 'this.printList::');
      this.$refs.print.EXCUTE_PRINT();
    },
    // 获取经手人
    getHandlerName(val) {
      let employeeName = '';
      console.log(val, 'valxxxx');
      this.handlerList.forEach(x => {
        // console.log(x, 'xxxxx');
        if (x.userId == val) {
          console.log(11111111111);
          employeeName = x.employeeName;
        }
      });
      return employeeName;
    },
    async getSalerList() {
      // this.$refs.table.START_LOADING();
      let emps = await getDealerUser();
      let obj = {};
      emps.data.map(item => {
        obj[item.userId] = item.employeeName;
      });
      this.handlerList = emps.data;
      console.log(emps, 'emps----');
      // this.fetchParams.xhrAbort = false;
      // this.$nextTick(() => {
      //   this.fetchParams.xhrAbort = true;
      // });
      // console.log(this.salerList, '销售列表');
    },
    async getPrintParams() {
      let params = { vModule: '90211009', vNormalCode: 'BJBY' };
      const res = await dealerPrintApi(params);
      if ((res.resultCode == 200) & res.data.length) {
        this.printParams = res.data[0];
      }

      console.log('打印的参数', this.printParams);
    },
    // 打印处理
    async printHandle() {
      try {
        if (this.selectColumns.length < 1) {
          return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
        }
        // 获取打印数据
        await this.getPrintData();
        this.showPrint = true;
        this.printList.profitNo = this.profitNo;
        console.log(this.printList);

        // this.printList.profitNo = this.profitNo;
        // 打印
        this.$nextTick(() => {
          this.$refs.print.EXCUTE_PRINT();
        });
      } catch (e) {
        console.log(e, 'e::');
      }
      // this.getPrintParams();
    },
    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },
    //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'配件报溢')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE + ''
      }));
    },
    // 操作员名称
    async getEmployee() {
      const res = await getAllUser();
      if (res.resultCode == 200) {
        res.data.map(e => {
          this.userList[e.userId] = e.employeeName;
        });
      }
      // console.log(res, '操作员名称');
      this.columns.find(item => item.dataIndex === 'OPERATOR_NAME').dictItems = this.columns.find(item => item.dataIndex === 'OPERATOR_NAME').filterItems = res.data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '入库单编号',
          dataIndex: 'PROFIT_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '原单编号',
        //   dataIndex: 'fake1',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '对应单编号',
          dataIndex: 'INVENTORY_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '入库方式',
        //   dataIndex: 'fake2',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '入库日期',
          dataIndex: 'FINISHED_DATE',
          // dateFormat: 'yyyy-MM-dd',
          sorter: true,
          sorter: true,
          filter: true,
          filterType: 'date-range',
          width: '160',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'd'
          }
        },
        // {
        //   title: '供应商名称',
        //   dataIndex: 'fake3',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '仓库名称',
          dataIndex: 'STORAGE_CODE',
          editItems: [],
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '税率',
        //   dataIndex: 'fake5',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
        {
          title: '操作员代码',
          dataIndex: 'UPDATED_BY',
          editable: false,
          editType: 'select',
          editItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          },
          sorter: true,
          filter: true,
          dictItems: [],
          filterType: 'checkbox',
          filterItems: [],
          showOverflowTooltip: true
        },
        {
          title: '配件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true,
          width: '160'
        },
        // {
        //   title: 'ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   dictItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '批号',
        //   dataIndex: 'PART_BATCH_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '发票号',
        //   dataIndex: 'fake7',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '配件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: 'CCC件类型',
        //   dataIndex: 'IS_CCC_PART',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '保质期',
        //   dataIndex: 'SHELF_LIFT_MARK'
        // },
        // {
        //   title: '发货单号',
        //   dataIndex: 'fake8',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '订货申请单号',
        //   dataIndex: 'fake9',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '发货中转库',
        //   dataIndex: 'fake10',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '计量单位',
        //   dataIndex: 'UNIT_NAME',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '入库数量',
          dataIndex: 'PROFIT_QUANTITY',
          summation: true,
          sorter: true,
          filter: true,
          precision: 2,
          filterType: 'number',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        // {
        //   title: '含税入库单价',
        //   dataIndex: 'PROFIT_PRICE',
        //   sorter: true,
        //   filter: true,
        //   precision: 2,
        //   filterType: 'number'
        // },
        {
          title: '含税入库单价',
          dataIndex: 'COST_PRICE',
          sorter: true,
          precision: 2,
          filter: true,
          filterType: 'number',
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        {
          title: '含税入库金额',
          dataIndex: 'PROFIT_AMOUNT',
          summation: true,
          precision: 2,
          sorter: true,
          filter: true,
          filterType: 'number',
          render: props => {
            props.row.PROFIT_AMOUNT = Number(props.row.COST_AMOUNT).toFixed(2);
            return <span>{props.row.PROFIT_AMOUNT}</span>;
          },
          showOverflowTooltip: true,
          exportFormat: {
            t: 'n'
          }
        },
        // {
        //   title: '含税成本金额',
        //   dataIndex: 'COST_AMOUNT',
        //   summation: true,
        //   precision: 2,
        //   sorter: true,
        //   filter: true,
        //   filterType: 'number',
        //   showOverflowTooltip: true,
        //   exportFormat: {
        //     t: 'n'
        //   }
        // },
        // {
        //   title: '库区代码',
        //   dataIndex: 'fake11',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        {
          title: '库位编号',
          dataIndex: 'STORAGE_POSITION_CODE',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        }
        // {
        //   title: '主组号',
        //   dataIndex: 'MAIN_GROUP_NO',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '备注',
        //   dataIndex: 'fake12',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '入库类型',
        //   dataIndex: 'fake13',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // }
        // {
        //   title: '发布单号',
        //   dataIndex: 'fake14',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true,
        //   showOverflowTooltip: true
        // },
        // {
        //   title: '销售经销商代码 ',
        //   dataIndex: 'fake15',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // }
      ];
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
